import { NextFunction, Request, Response } from "express";
import createError from "http-errors";
import errorHandler from "./middlewares/errorHandler";
import verificationToken from "./middlewares/verificationToken";
import { IModuleList } from "./types/IModelList";

import systemModuleList from "./system/index";
import payModuleList from "./pay/index";
import codeModuleList from "./codeGeneration/index";
import HomePage from "./HomePage/index";
import Cat from "./pet/index";
import utilsRouter from "./system/utilsRouter"

const apiList: IModuleList[][] = [systemModuleList, payModuleList, codeModuleList, HomePage, Cat];

export function createRouter(app: any) {
  // 验证token
  app.use("/", verificationToken);
  app.use("/api/utils/handler", utilsRouter)

  // 注册各模块
  for (const module of apiList) {
    for (const item of module) {
      app.use(item.url, item.router);
    }
  }

  // 404
  app.use((_req: Request, _res: Response, next: NextFunction) => {
    next(createError(404));
  });

  // 错误处理
  app.use(errorHandler);
}
